package com.persondemo.videoappliction.ui.vip;

import android.app.AlertDialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.persondemo.videoappliction.R;
import com.persondemo.videoappliction.bean.DelteShopBean;
import com.persondemo.videoappliction.bean.MeShopBean;
import com.persondemo.videoappliction.component.ApplicationComponent;
import com.persondemo.videoappliction.component.DaggerHttpComponent;
import com.persondemo.videoappliction.event.PutShopEvent;
import com.persondemo.videoappliction.ui.adapter.PutShopAdapter;
import com.persondemo.videoappliction.ui.base.AppManager;
import com.persondemo.videoappliction.ui.base.BaseActivity;
import com.persondemo.videoappliction.ui.dianping.DianPingDetilsActivity;
import com.persondemo.videoappliction.ui.vip.contract.MeShopContract;
import com.persondemo.videoappliction.ui.vip.presenter.MeShopPresenter;
import com.persondemo.videoappliction.utils.PreferenceUtil;
import com.persondemo.videoappliction.utils.T;
import com.persondemo.videoappliction.widget.ClearEditText;
import com.persondemo.videoappliction.widget.CustomLoadMoreView;

import org.simple.eventbus.EventBus;
import org.simple.eventbus.Subscriber;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.srain.cube.views.ptr.PtrClassicFrameLayout;
import in.srain.cube.views.ptr.PtrDefaultHandler;
import in.srain.cube.views.ptr.PtrFrameLayout;
import in.srain.cube.views.ptr.PtrHandler;

public class MeShopActivity extends BaseActivity<MeShopPresenter> implements MeShopContract.View {
    @BindView(R.id.put_shop_back)
    ImageView putShopBack;
    @BindView(R.id.put_shop_text)
    TextView putShopText;
    @BindView(R.id.detilsdetils_recycle02)
    RecyclerView mRecyclerView;
    @BindView(R.id.SimpleMultiStateView)
    com.persondemo.videoappliction.widget.SimpleMultiStateView SimpleMultiStateView;
    @BindView(R.id.mPtrFrameLayout)
    PtrClassicFrameLayout mPtrFrameLayout;
    private PutShopAdapter adapter;
    int page = 1;
    private String imei = "";
    private List<MeShopBean.DataBean> list;

    @Override
    public void onRetry() {

    }

    @Override
    public int getContentLayout() {
        return R.layout.module_activity_meshop;
    }

    @Override
    public void initInjector(ApplicationComponent appComponent) {
        DaggerHttpComponent.builder()
                .applicationComponent(appComponent)
                .build()
                .inject(this);
    }

    @Subscriber
    private void back(PutShopEvent putShopEvent) {
        if (putShopEvent == null) return;
        String title = putShopEvent.getTitle();
        if (title.equals("1")) {
            if (mPresenter != null) {
                showLoadingDialog();
                page = 1;
                mPresenter.getshop(imei, page + "");
            }
        }
    }

    @Override
    public void bindView(View view, Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        mPtrFrameLayout.disableWhenHorizontalMove(true);
        mPtrFrameLayout.setPtrHandler(new PtrHandler() {
            @Override
            public boolean checkCanDoRefresh(PtrFrameLayout frame, View content, View header) {
                return PtrDefaultHandler.checkContentCanBePulledDown(frame, mRecyclerView, header);
            }

            @Override
            public void onRefreshBegin(PtrFrameLayout frame) {
                if (mPresenter != null) {
                    showLoadingDialog();
                    page = 1;
                    mPresenter.getshop(imei, page + "");
                }
            }
        });
        mRecyclerView.setLayoutManager(new GridLayoutManager(MeShopActivity.this, 1));
        adapter = new PutShopAdapter(MeShopActivity.this, list);
        mRecyclerView.setAdapter(adapter);
        adapter.setEnableLoadMore(true);
        adapter.setLoadMoreView(new CustomLoadMoreView());
        adapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                godeitls(list.get(position).getId() + "");
            }
        });
        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                showpop(position);
            }
        });
        adapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                mPresenter.getshop(imei, page + "");
            }
        }, mRecyclerView);
    }

    private void showpop(int position) {
        AlertDialog.Builder builder = new AlertDialog.Builder(MeShopActivity.this);
        final AlertDialog dialog;
        View dialgview = LayoutInflater.from(MeShopActivity.this).inflate(R.layout.item_delte, null, false);
        dialog = builder.setView(dialgview).create();
        if (dialog.isShowing()) {
            dialog.dismiss();
        } else {
            dialog.show();
        }
        dialgview.findViewById(R.id.dilog_nickname_reset_cancel).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        });
        dialgview.findViewById(R.id.dilog_nickname_reset_comfrim).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mPresenter!=null&&list.size()>0){
                    mPresenter.delteshop(imei,list.get(position).getId()+"");
                }
                adapter.remove(position);
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            }
        });
    }

    private void godeitls(String ids) {
        Intent intent = new Intent(MeShopActivity.this, DianPingDetilsActivity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id", ids);
        intent.putExtras(bundle);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
    }

    @Override
    public void initData() {
        list = new ArrayList<>();
        imei = PreferenceUtil.getString("imei", "");
        if (TextUtils.isEmpty(imei)) {
            T.showShort(MeShopActivity.this, "未获取到imei权限，请从设置打开授予权限！");
            return;
        }
        if (mPresenter != null) {
            showLoadingDialog();
            mPresenter.getshop(imei, page + "");
        }
    }

    @Override
    protected void onDestroy() {
        EventBus.getDefault().unregister(this);
        super.onDestroy();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.put_shop_back, R.id.put_shop_text})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.put_shop_back:
                AppManager.getAppManager().finishActivity();
                break;
            case R.id.put_shop_text:
                Intent intent = new Intent(MeShopActivity.this, PutShopActivity.class);
                startActivity(intent);
                overridePendingTransition(R.anim.slide_in_left, R.anim.slide_in_right);
                break;
        }
    }

    @Override
    public void loadshop(MeShopBean meShopBean) {
        hideLoadingDialog();
        if (meShopBean.getStatus() == 1) {
            if (meShopBean.getData() == null || meShopBean.getData().size() == 0) {
                T.showShort(MeShopActivity.this, "暂时还没有数据，请点击右上角添加商户信息！");
                return;
            }
            if (meShopBean.getData() != null && meShopBean.getData().size() > 0) {
                list.clear();
                list = meShopBean.getData();
                adapter.setNewData(list);
                mPtrFrameLayout.refreshComplete();
                showSuccess();
                page++;
                if (meShopBean.getData().size() < 1) {
                    adapter.loadMoreEnd();
                }
            }
        } else {
            T.showShort(MeShopActivity.this, meShopBean.getMsg());
        }
    }

    @Override
    public void loadmore(MeShopBean meShopBean) {
        if (meShopBean.getStatus() == 1 && meShopBean.getData().size() != 0) {
            page++;
            adapter.addData(meShopBean.getData());
            adapter.loadMoreComplete();
        } else if (meShopBean.getData() == null || meShopBean.getData().size() == 0) {
            adapter.loadMoreEnd();
            T.showShort(MeShopActivity.this, "没有更多数据了！");
            return;
        } else {
            T.showShort(MeShopActivity.this, meShopBean.getMsg());
        }
    }

    @Override
    public void loaddetle(DelteShopBean delteShopBean) {
          if (delteShopBean.getStatus()==1){
              T.showShort(MeShopActivity.this,"删除成功");
          }else {
              T.showShort(MeShopActivity.this,delteShopBean.getMsg());
          }
    }
}
